package SocketTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoService {
    public static void main(String[] args) {
        //服务类需要用ServerSocket类
        try {
            ServerSocket serverSocket = new ServerSocket(1024,5);
            System.out.println("等待客户端连接......");
            //阻塞 等待客户端连接
            Socket client = serverSocket.accept();
            //输出
            PrintWriter printWriter = new PrintWriter(client.getOutputStream(), true);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            String userIn;
            while (((userIn = bufferedReader.readLine()) != null)) {
                System.out.println("收到客户端端消息:"+userIn);
                //发回给客户端
                printWriter.write(userIn);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
